/**
 * <p>
 * 版权所有：中企动力
 * </p>
 * <p>
 * 未经本公司许可，不得以任何方式复制或使用本程序任何部分
 * </p>
 */
package com.ce.pms.report.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ce.pms.report.queryvo.MonthChargeQueryVo;
import com.ce.pms.report.vo.BaseReportVo;
import com.ce.pms.report.vo.MonthChargeVo;
import com.ce.report.entity.CEReportDataSet;
import com.ce.report.exception.CEReportException;
import com.ce.report.service.ICEReportDataSetService;
import com.ce.report.vo.ReportsQueryBaseVo;
import com.ce.util.DateUtil;

/**
 *
 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
 * @Created 2008-12-4
 */

public class MonthChargeService extends BaseReportService implements
		ICEReportDataSetService {

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * com.ce.pms.report.service.BaseReportService#converToReportVoList(java
	 * .util.List)
	 */
	@Override
	protected List<BaseReportVo> converToReportVoList(List<Object[]> dataList) {
		List<BaseReportVo> list = new ArrayList<BaseReportVo>();

		for (Object[] obj : dataList) {
			MonthChargeVo voTmp = new MonthChargeVo();
			voTmp.setItem((String) obj[0]);
			voTmp.setCurrentReceivable(setValue(obj[1]));
			voTmp.setLastMonthArrearage(setValue(obj[2]));
			voTmp.setCurrentCharge(setValue(obj[3]));
			voTmp.setCurrentAdjust(setValue(obj[4]));
			voTmp.setCurrentDerate(setValue(obj[5]));

			voTmp.setCurrentArrearage(((BigDecimal) obj[1]).add(
					(BigDecimal) obj[2]).subtract((BigDecimal) obj[3]).add(
					(BigDecimal) obj[4]).add((BigDecimal) obj[4]));

			list.add(voTmp);
		}
		return list;
	}

	/**
	 * 报表头信息
	 *
	 * @param queryVO
	 * @return
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2008-11-28
	 */
	private Map<String, Object> getHeadInfo(MonthChargeQueryVo queryVo) {

		Date month = queryVo.getMonthDate();

		Map<String, Object> headerMap = new HashMap<String, Object>();
		headerMap.put("reportHeadStr", "（月结时间："
				+ DateUtil.convertDateToStr(month, "yyyy年MM月") + "）");

		// 其他信息
		List<Object[]> data = reportDao.getDataList("monthChargeType", DateUtil
				.convertDateToStr(month, "yyyyMM"), queryVo.getPrecinct());

		for(int i=0;i<data.size();i++) {
			headerMap.put("key"+i, (String)(data.get(i)[0]));
			headerMap.put("value"+i, (BigDecimal)(data.get(i)[1]));
		}

		return headerMap;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * com.ce.report.service.ICEReportDataSetService#getReportDataSet(com.ce
	 * .report.vo.ReportsQueryBaseVo)
	 */
	public CEReportDataSet getReportDataSet(ReportsQueryBaseVo arg0)
			throws CEReportException {
		MonthChargeQueryVo queryVo = (MonthChargeQueryVo) arg0;

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("period",DateUtil.convertDateToStr(queryVo.getMonthDate(), "yyyyMM") );
		map.put("precinctId",queryVo.getPrecinct());

		List<Object[]> data = reportDao.getDataList("monthCharge",map);

		// 报表数据准备
		List<BaseReportVo> list = converToReportVoList(data);

		return new CEReportDataSet(list, getHeadInfo(queryVo));
	}
}
